﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using FlotasDesktop;

namespace GestorDeFlotasDesktop.Facturar
{
    public partial class Facturar : Form
    {
        String conexion;
        public Facturar()
        {
            InitializeComponent();
            Utils utils = new Utils();
            conexion = utils.cargarProperties();
            desdePicker.CustomFormat = "dd/MM/yyyy HH:mm:ss";
            desdePicker.Format = DateTimePickerFormat.Custom;
            hastaPicker.CustomFormat = "dd/MM/yyyy HH:mm:ss";
            hastaPicker.Format = DateTimePickerFormat.Custom;
            this.cargarClientes();
            this.mostrarFacturaciones();
        }

        private void cargarClientes()
        {
            using (SqlConnection connection = new SqlConnection(conexion))
            {
                try
                {
                    using (SqlCommand command = new SqlCommand())
                    {
                        command.Connection = connection;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "TEQUILASO.sp_clienteHabilitado";
                        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                        {
                            DataTable dt = new DataTable();
                            adapter.Fill(dt);
                            DataColumn dcFullName = new DataColumn();
                            dcFullName.DataType = System.Type.GetType("System.String");
                            dcFullName.ColumnName = "FullName";
                            dcFullName.Expression = "Apellido + ', ' + Nombre";
                            dt.Columns.Add(dcFullName);
                            
                            clienteComboBox.DataSource = dt;
                            clienteComboBox.DisplayMember = "FullName";
                            clienteComboBox.ValueMember = "id";
                        }
                        
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            using (SqlConnection connection = new SqlConnection(conexion))
            {
                try
                {
                    using (SqlCommand command = new SqlCommand())
                    {
                        connection.Open();
                        command.Connection = connection;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "TEQUILASO.sp_facturar";
                        command.Parameters.AddWithValue("@Id_cliente", clienteComboBox.SelectedValue);
                        command.Parameters.AddWithValue("@fecha_desde", desdePicker.Value);
                        command.Parameters.AddWithValue("@fecha_hasta", hastaPicker.Value);
                        command.ExecuteReader();
                        MessageBox.Show("Operacion realizada con exito");
                        this.mostrarFacturaciones();
                        connection.Close();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void mostrarFacturaciones(){
            using (SqlConnection connection = new SqlConnection(conexion))
            {
                try
                {
                    using (SqlCommand command = new SqlCommand())
                    {
                        connection.Open();
                        command.Connection = connection;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "TEQUILASO.sp_verFacturacionCliente";
                        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                        {
                            DataSet ds = new DataSet();
                            adapter.Fill(ds);
                            dataGridView1.DataSource = ds.Tables[0];

                        }


                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void itemsDeLaFacturacion(int idFacturacion)
        {
            using (SqlConnection connection = new SqlConnection(conexion))
            {
                try
                {
                    using (SqlCommand command = new SqlCommand())
                    {
                        connection.Open();
                        command.Connection = connection;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "TEQUILASO.sp_buscarItemsFacturacion";
                        command.Parameters.Add(new SqlParameter("@Id_Factura_Cliente", idFacturacion));
                        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                        {
                            DataSet ds = new DataSet();
                            adapter.Fill(ds);
                            dataGridView2.DataSource = ds.Tables[0];
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            dataGridView1.CurrentRow.Selected = true;
            int facturacionId = (int)dataGridView1.SelectedRows[0].Cells[0].Value;
            this.itemsDeLaFacturacion(facturacionId);
        }
    }
}
